package com.appcenter.message.service;

import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.message.input.*;
import com.appcenter.message.mapper.MessageTargetsMapper;
import com.appcenter.message.mapper.MessagesMapper;
import com.appcenter.message.model.MessageTargets;
import com.appcenter.message.model.Messages;
import com.appcenter.message.output.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MessagesService {
    @Autowired
    MessagesMapper MsgMapper;
    @Autowired
    MessageTargetsMapper MsgTargetMapper;

    @Autowired
    com.appcenter.message.expand.DingDingMsg DingDingMsg;

    ///发送消息服务
    public SendMessageOutput SendMessage(SendMessageInput input) throws Exception {
        if(input.getTitle()==null||input.getTitle().equals("")){
            throw new ErrorJsonResult("1000010","消息标题不能为空");
        }
        if(input.getSendMessageTargets()==null||input.getSendMessageTargets().size()==0){
            throw new ErrorJsonResult("1000011","消息接收人不能为空");
        }
        if(input.getMessageType()==null||input.getMessageType().equals("")){
            throw new ErrorJsonResult("1000012","消息类型不能为空");
        }
        SendMessageOutput output=new SendMessageOutput();
        String toNames = "";

        String toUserCode = "";

        for (SendMessageTargetInput item : input.getSendMessageTargets()) {
            toNames += item.getTargetUserName() + ',';
            toUserCode += item.getTargetUserCode() + ',';
        }
        toNames = toNames.substring(0,toNames.length()-1);
        if (input.getMessageType().equals("站内信"))
        {

        }
        else if (input.getMessageType().equals( "电子邮件"))
        {
            //_IUserSmtpEmailSender.Send(toUserCode, input.Title, input.MsContent);
        } else if (input.getMessageType().equals("钉钉消息"))
        {
            //_IUserSmtpDingSender.Send(toUserCode, input.Title, input.MsContent, input.MobileUrl, "", input.FromAppCode);
            String Result=DingDingMsg.Send(toUserCode, input.getTitle(), input.getMsContent(), input.getMobileUrl(), "", input.getFromAppCode());

        }
        else
        {
            output.setMsgResult(input.getMessageType() + "--暂不支持此类型");
            throw new ErrorJsonResult("1000013",input.getMessageType() + "--暂不支持此类型");
        }
        Messages MsgModel=new Messages();
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        MsgModel.setId(uuid);
        MsgModel.setTitle(input.getTitle());
        MsgModel.setMsContent(input.getMsContent());
        MsgModel.setPCUrl(input.getPCUrl());
        MsgModel.setMobileUrl(input.getMobileUrl());
        MsgModel.setMessageType(input.getMessageType());
        MsgModel.setIsSend(true);
        MsgModel.setBusinessType(input.getBusinessType());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        MsgModel.setCreateTime(df.format(new Date()));
        MsgModel.setCreateUserId(input.getCreateUserId());
        MsgModel.setCreateUserCode(input.getCreateUserCode());
        MsgModel.setCreateUserName(input.getCreateUserName());
        MsgModel.setFromAppCode(input.getFromAppCode());
        MsgModel.setFromAppCode(input.getToAppCode());
        MsgModel.setToNames(toNames);
        MsgMapper.addMessages(MsgModel);


        for (SendMessageTargetInput item : input.getSendMessageTargets()) {
            MessageTargets MessageTarget=new MessageTargets();
            String TargetUUid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            MessageTarget.setId(TargetUUid);
            MessageTarget.setMessageId(uuid);
            MessageTarget.setCreateTime(df.format(new Date()));
            MessageTarget.setTargetUserCode(item.getTargetUserCode());
            MessageTarget.setTargetUserName(item.getTargetUserName());
            MessageTarget.setIsRead(false);
            MessageTarget.setIsSend(true);
            MessageTarget.setSendTime(df.format(new Date()));
            MsgTargetMapper.addMessageTargets(MessageTarget);
        }
        output.setItem(MsgModel);
        output.setMsgResult("发送成功");
        return  output;
    }


    public ReadMessageOutput ReadMessage(ReadMessageInput input){

        ReadMessageOutput output = new ReadMessageOutput();
        MessageTargets curMessageTarget = MsgTargetMapper.getMessageTargets(input.getMessageTargetId());

        if (curMessageTarget == null)
        {
            throw new ErrorJsonResult("1000014","未找到消息信息");
         }
        if (!curMessageTarget.getTargetUserCode().toUpperCase().equals(input.getUserId().toUpperCase()))
        {
            throw new ErrorJsonResult("1000015","用户信息错误,请联系管理员");
        }
        if (curMessageTarget.getIsRead())
        {
            throw new ErrorJsonResult("1000016","用户已读");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        curMessageTarget.setIsRead(true);
        curMessageTarget.setReadTime(df.format(new Date()));
        MsgTargetMapper.ReadMsg(curMessageTarget);
        output.setResult(true);
        output.setResultMsg("成功");
        return output;
    }

    public GetUnreadMessageCountOutput GetUnreadMessageCount(GetUnreadMessageCountInput input){
        GetUnreadMessageCountOutput output =new GetUnreadMessageCountOutput();
        List<MessageTargets> list = MsgTargetMapper.getUnreadMessageCount(input);
        output.setCount(list.size());
        return output;
    }

    public SearchMessageListOutput SearchMessageList(SearchMessageListInput input){
        SearchMessageListOutput output = new SearchMessageListOutput();
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"CreateTime desc");
        List<MessageTargets> ls = MsgTargetMapper.GetAllMessageTargets(input);
        PageInfo<MessageTargets> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<MessageTargets>(page.getList(),(int)page.getTotal()));
        return  output;
    }


    public LoadMessageListPageOutput LoadManageMessageListPage(LoadMessageListPageInput input){
        LoadMessageListPageOutput output = new LoadMessageListPageOutput();
        output.setItems(SearchMessageList(input).getItems());
        return output;
    }





}
